
package com.videoweb.interceptor;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.minidev.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.videoweb.utils.HttpCode;
import com.videoweb.utils.InstanceUtil;
import com.videoweb.utils.Jwt;

public class TokenInterceptor  implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		arg1.setCharacterEncoding("utf-8");

		if (arg0.getRequestURI().contains("/ying/getVideoDetailV2")
				|| arg0.getRequestURI().contains("/ying/getVideoCommonV2")
				|| arg0.getRequestURI().contains("/ying/getFindVideoV2")
				|| arg0.getRequestURI().contains("/qiniu/getQiniuVedioKey")
				|| arg0.getRequestURI().contains("/ying/getMemberInfoV2")
				|| arg0.getRequestURI().contains("/openapi/queryDataByType")){
			return true;
		}

        String token = arg0.getHeader("token");
		getHeadersInfo(arg0);

        if(token  != null  && !"".equals(token)) {
        	 Map<String,Object> resultMap = new HashMap<String,Object>();
        	 resultMap = Jwt.validToken(token);
        	 if(resultMap != null && !"".equals(resultMap)) {
        		 boolean flag = (boolean) resultMap.get("isSuccess");
        		 if(flag) {
        			 return true;
        		 } else {
        			 arg1.setContentType("text/html; charset=UTF-8");
                     Map<String, Object> modelMap = InstanceUtil.newLinkedHashMap();
                     modelMap.put("httpCode", HttpCode .TIMEOUTTOKEN.value());
                     modelMap.put("msg", HttpCode.TIMEOUTTOKEN.msg());
                     modelMap.put("timestamp", System.currentTimeMillis());
                     PrintWriter out = arg1.getWriter();
                     out.println(JSON.toJSONString(modelMap));
                     out.flush();
                     out.close();
                     return false;
        		 }
        	 }
        	 
        }
        arg1.setContentType("text/html; charset=UTF-8");
        Map<String, Object> modelMap = InstanceUtil.newLinkedHashMap();
        modelMap.put("httpCode", HttpCode.LOSTTOKEN.value());
        modelMap.put("msg", HttpCode.LOSTTOKEN.msg());
        modelMap.put("timestamp", System.currentTimeMillis());
        PrintWriter out = arg1.getWriter();
        out.println(JSON.toJSONString(modelMap));
        out.flush();
        out.close();
		return false;
	}

	private void getHeadersInfo(HttpServletRequest request) {
		Enumeration headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = (String) headerNames.nextElement();
			String value = request.getHeader(key);
//			System.out.println("header:"+key+"="+value);
		}
	}


}
